// userPageC/pages/vouchersDetail/vouchersDetail.js
const app = getApp(); 
const WXAPI = require("../../../api/newApi"); 
const WXAPIOLD =  require("../../../api/api"); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '优惠券详情', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: 'linear-gradient(0deg, #FE7135 0%, #FF8B03 98.66%);', // 导航栏背景颜色
      isloading: true,
    },
    id: '',
    infoData: {},
    redemptionHttp: app.globalData.redemptionHttp,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var id = options.id
    this.setData({
      id: id
    })
    this.getDetail()
  },
  getDetail(){
    var id = this.data.id
    var url = `/app/v1/customer/coupon/detail/${id}`
    WXAPI.couponDetail(url, '', res => {
      this.setData({
        infoData: res.result
      })
    })
  },
  //可用门店
  handleShop(){
    var url = this.data.redemptionHttp + 'pages/shopDetail/shopDetail'
    var obj = {
      id: this.data.infoData.couponId,
      time: +new Date().getTime(),
    }
    obj = JSON.stringify(obj)
    wx.navigateTo({
      url: '/userPageC/pages/externalH5/externalH5?url='+url+'&data='+obj+'&type=true',
    })
  },
  //唤醒扫一扫功能
  getScancode: function() {   
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        let parameter = res.result.split("code=")[1];
        console.log(parameter)
        if (parameter==undefined) {
          wx.showToast({
            title: '无效的二维码',
          })
          return
        }
        let result = JSON.parse(parameter)

        console.log(result)
        console.log("扫一扫参数",result.param)
        console.log("扫一扫类型",result.type) 
        console.log("扫一扫类型",result.staffId)
        if(result.type==1){
          wx.navigateTo({
            url: '/userPageA/pages/scan/information/information?param=' + result.param
          })
        }else if(result.type==2){
          wx.navigateTo({
            url: '/userPageA/pages/scan/receiving_assets/receiving_assets?param=' + result.param
          })
        }else if(result.type==4){
          this.setData({
            qrCodeUrl: result.param,
            staffId: result.staffId
          })
          this.getParCollectionQrCodeUrl()
        }
        else if(result.type==5){ //退卡页面
          wx.navigateTo({
            url: '/userPageB/pages/returnCard/returnCard?param=' + result.param
          })
        }
        else{
          console.log('else')
        }
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //解析二维码confirmInfo
  getParCollectionQrCodeUrl(){
   
    var data = {
      qrCodeUrl:this.data.qrCodeUrl,
    }
    WXAPIOLD.parCollectionQrCodeUrl(data, (res) =>{
      const data = res.result
      console.log("解析二维码",res)
      if(res.code === 200){
        var merchantId = data.merchantId
        console.log('this.data.staffId',this.data.staffId)
        wx.navigateTo({
          // url: '/pages/pay/pay?QRmerchantId='+merchantId,   
          url: '/pages/pay/payTransit/payTransit?QRmerchantId='+merchantId+'&staffId='+this.data.staffId, 
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})